\documentclass[t,12pt,aspectratio=169]{beamer} % 16:9 宽屏比例，适合现代投影
\usepackage{ctex} % 中文支持
\usepackage{amsmath, amssymb} % 数学公式与符号
\usepackage{graphicx}
\usepackage{url}
\usepackage{verbatim}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 插入代码
\usepackage{listings}
\usepackage{color}

% 设置列表的样式
\definecolor{codegreen}{rgb}{0,0.6,0}
\definecolor{codegray}{rgb}{0.5,0.5,0.5}
\definecolor{codepurple}{rgb}{0.58,0,0.82}
\definecolor{backcolour}{rgb}{0.95,0.95,0.92}

\lstdefinestyle{mystyle}{
    backgroundcolor=\color{backcolour},   
    commentstyle=\color{codegreen},
    keywordstyle=\color{magenta},
    numberstyle=\tiny\color{codegray},
    stringstyle=\color{codepurple},
    basicstyle=\ttfamily\footnotesize,
    breakatwhitespace=false,         
    breaklines=true,                 
    captionpos=b,                    
    keepspaces=true,                 
    numbers=left,                    
    numbersep=5pt,                  
    showspaces=false,                
    showstringspaces=false,
    showtabs=false,                  
    tabsize=2
}

\lstset{style=mystyle}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 主题设置（推荐简洁风格）
\usetheme{Madrid}
\usecolortheme{default} % 可选：seahorse, beaver, dolphin 等

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\title{R语言统计入门第2章：R语言的运行环境}
\author{PD ET AL}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{document}

\begin{frame}
  \titlepage
\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%\begin{frame}{目录 }
%
%\begin{enumerate}
%\item[2.1.] 会话管理 Session management 
%\item[2.2.] 绘图系统 The graphics subsystem 
%\item[2.3.] R语言编程 R programming 
%\item[2.4.] 数据输入 Data entry 
%\item[2.5.] 书中习题 Exercises 
%\item[2.6.] 练习（选择题）
%\item[2.7.] 练习 （简答题）
%
%\end{enumerate}
%
%\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}{目录 }

\begin{enumerate}
\item[2.1.] 会话管理：工作空间、文本输出、脚本、帮助、包、内置数据、载入和卸载数据框、对数据框的操作
\item[2.2.] 绘图系统：图形布局、增添组件、子图、组合图形
\item[2.3.] R语言编程：流程控制、类、类函数
\item[2.4.] 数据输入：读取和保存文本文件、数据编辑、其它程序接口
\item[2.5.] 书中习题
\item[2.6.] 练习（选择题）
\item[2.7.] 练习（简答题）

%\item  会话管理，作图系统，R编程，数据读写
%\item  工作空间，脚本，帮助，载入包，流程控制，类函数，读写文件

\end{enumerate}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}{课程讲解重点难点 }

\begin{enumerate}
\item 使用attach, detach, subset, transform和within函数操作数据框。
\item 使用plot函数画一元函数图像和散点图。
\item 编写循环语句(for, while, repeat, implicit)，编写和运行脚本程序。
\item 使用read.table函数读入数据。

\end{enumerate}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{2.1.1.a. 工作空间 The workspace }

\begin{itemize}
\item {\color{red}问题：列出工作空间里的所有变量的名称。}
\item 解答：使用函数\, {\color{blue}\verb+ls()+ }, 返回一个字符串向量，由工作空间里的所有变量的名称组成。ls = list. 
这是一个 UNIX 命令。

\item {\color{red}问题：如何删除工作空间里的一些变量？}
\item 解答：使用函数\, {\color{blue}\verb+rm()+ }, 参数是变量名。rm = remove, 也是 UNIX 命令。

\item {\color{red}问题：如何删除工作空间里的所有变量？}
\item 解答：{\color{blue}\verb+rm(list=ls())+ }, 或者点击菜单。

\item {\color{red}问题：如何将工作空间里的所有变量保存到一个文件，以便下次使用？}
\item 解答：{\color{blue}\verb+save.image(file='mydata.RData')+}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{2.1.1.b. 工作空间 The workspace }

\begin{itemize}
\item {\color{red}问题：举例说明上一页的几个函数。}

\begin{lstlisting}[language=R]
rm(list=ls())  #删除工作空间里的所有变量
ls()           #显示工作空间里的所有变量，括号不能省略
#character(0)    #（结果没有任何变量）

x<-1; y<-c(2,3,4)  #用分号隔开，可以同时运行几个命令
ls()
#[1] "x" "y"      #（工作空间里现在有2个变量）

rm(x)           #删除变量 x
ls()
#[1] "y"         #（工作空间里没有了变量 x）
\end{lstlisting}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{2.1.2.a. 保存输出结果、当前工作目录}

\begin{itemize}
\item {\color{red}问题：如何保存图像等其它输出结果？} \\
回答：使用菜单。

%\item {\color{red}问题：介绍一下 Emacs Speaks Statistics? }\\ 
%回答：Linux user? Good!

%\item {\color{red}问题：如何将运行结果保存到文件里？}\\
% 回答： 使用函数\, {\color{blue}\verb+sink()+}.
%%{\color{blue}\verb+ > sink('myfile')+}\\
%%{\color{blue}\verb+ > sink()+} 

\item {\color{red}问题：如何获取当前目录名称？} \\
回答：使用函数\, {\color{blue}\verb+getwd()+}, 或者点击菜单。

\item {\color{red}问题：如何改变当前目录？}\\
回答：使用函数\, {\color{blue}\verb+setwd()+}, 或者点击菜单。

\item {\color{red}问题：如何查看当前目录中的所有文件} \\
回答：使用函数\, {\color{blue}\verb+dir()+}. 

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{2.1.2.b.  保存输出结果、当前工作目录（例子） }

\begin{itemize}
\item {\color{red}问题：举例说明上一页的几个函数。}

{\footnotesize\color{blue}
\begin{verbatim}
> getwd()  #获取当前工作目录
[1] "/Users/liqingsimac"  （这是macOS里的Home目录）
> dir()  #查看当前目录里的文件
[1] "Desktop"   "Documents" "Downloads" "Library"   "Movies"    "Music"    
[7] "Pictures"  "Public"   
> ls()  #显示工作空间里的所有变量
[1] "aa" "x"  "y" 
> sink('myfile')  #将接下来的命令的结果保存到文件 myfile 里
> ls()  #注意到这个命令之后没有显示任何结果
> sink()  #解除保存
> dir()  #查看当前目录，有结果显示，而且多了一个文件 myfile
[1] "Desktop"   "Documents" "Downloads" "Library"   "Movies"    "Music"    
[7] "Pictures"  "Public"    "myfile" 
\end{verbatim}
}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{2.1.3. 写脚本 scripting }

\begin{itemize}
\item {\color{red}问题：单词 script, scribe, scribble, scrabble 分别是什么含义？}
\item 解答：需要查字典。script: 剧本、脚本、手稿。

\item {\color{red}问题：如何写一段程序脚本，保存在当前目录，并运行？}
\item 解答：打开脚本编辑器，编辑保存并运行。

其它集成开发环境 TINN-R, WinEdt, ESS, ... , 也能编译 R 程序。

RStudio 也是很好的集成开发环境。

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{2.1.4.a. 如何找到帮助？Getting help }

\begin{itemize}
\item {\color{red}问题：如何查找R软件的内建帮助文档的总页面？}

\item 解答：使用\, {\color{blue}\verb+help.start()+}. 注意页面的网址，是 127.0.0.1, 说明这是本地文档。这些链接到的页面都在R软件的安装目录里。

\item {\color{red}问题：如何查找某函数的内建帮助文档？}

\item 解答：使用问号与函数名的方式，例如\, {\color{blue}\verb+?rnorm+} 将返回一个页面，是这个函数\, {\color{blue}\verb+rnorm()+} 的使用说明书。这几乎是最有用的帮助文档。

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{2.1.4.b. R软件自带的使用说明，建议多多阅读！}

{\footnotesize\color{blue}
\begin{verbatim}
> help.start()
\end{verbatim}
}

\begin{center}
\includegraphics[height=0.5\textheight, width=0.9\textwidth]{help-start.png}
\end{center}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{2.1.5. 程序包 Packages }

\begin{itemize}
\item {\color{red}问题：什么是程序包？}

\item 解答：
\begin{itemize}
\item  A package can contain 
\begin{enumerate}
\item  {\color{purple}functions} written in the R language, 
\item  dynamically loaded {\color{purple}libraries} of compiled code (written in C or Fortran mostly), 
\item  and {\color{purple}data sets}. 
\end{enumerate}

\item It generally implements functionality that most users will probably not need to have loaded all the time. 
\end{itemize}

\item {\color{red}问题：如何载入 \, {\color{blue}\verb+survival+} 程序包？}

\item 解答：\, {\color{blue}\verb+> library(survival)+}


\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{2.1.6.a. 内建数据 }

\begin{itemize}
\item {\color{red}问题：查看程序包 \,{\color{blue} ISwR} 的数据框 \,{\color{blue}\verb+thuesen+}.}

\item 解答：这个数据框包含了24个糖尿病人的心室收缩速度 (short.velocity) 和血糖含量 (blood.glucose)。
首先使用 \,{\color{blue}\verb+library()+} 函数载入这个程序包，然后查看这个程序包里的 \,{\color{blue}\verb+thuesen+} 数据框。
{\color{blue}
\begin{verbatim}
> library(ISwR)
> thuesen
   blood.glucose short.velocity
1           15.3           1.76
2           10.8           1.34
...
23           8.8           1.12
24           9.5           1.70
\end{verbatim}
}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{2.1.6.b. 内建数据 }

\begin{itemize}

\item {\color{red}问题：R软件发行版自带哪些数据？}

\item 解答：出于写各个R函数的说明文档的需要，R软件也自带一些数据框。例如 \, {\color{blue}\verb+airquality+}. 
这个数据框包含了1973年5月到9月的纽约的空气质量数据，共153天的数据。
{\color{blue}
\begin{verbatim}
> airquality
    Ozone Solar.R Wind Temp Month Day
1      41     190  7.4   67     5   1
2      36     118  8.0   72     5   2
...
153    20     223 11.5   68     9  30
> ?airquality  #查看该数据框的帮助文档
starting httpd help server ... done  （打开了一个帮助页面）
\end{verbatim}
}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{2.1.7.a. 引用数据框里的数据 attach and detach }

\begin{itemize}
\item {\color{red}问题：引用数据框\, {\color{blue}\verb+thuesen+} 中的两列数据，绘制散点图。}

\item 解答：使用 \,{\color{blue}\verb+$+} 连接数据库名称和列名称。
\begin{lstlisting}[language=R]
plot(thuesen$blood.glucose, thuesen$short.velocity)
\end{lstlisting}

\item {\color{red}问题：如何直接引用数据框\, {\color{blue}\verb+thuesen+} 中的数据？}

\item 解答：如果预先 {\color{blue}\verb+attach+} 了一个数据框，那么之后就可以直接引用该数据框里的变量（列名称）了。
\begin{lstlisting}[language=R]
attach(thuesen)  # 把这个数据框载入到工作空间
plot(blood.glucose,short.velocity)  #引用数据框里的变量
detach(thuesen)  # 把这个数据框从工作空间里移走
\end{lstlisting}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{2.1.7.b. 查看R软件的搜索路径 }

\begin{itemize}

\item {\color{red}问题：如何查看R软件的搜索路径？}

\item 解答：使用 {\color{blue}\verb+search()+} 函数。
{\footnotesize\color{blue}
\begin{verbatim}
> search()  #可以发现有 ISwR 这个 package
 [1] ".GlobalEnv"        "package:ISwR"      "tools:RGUI"       
 [4] "package:stats"     "package:graphics"  "package:grDevices"
 [7] "package:utils"     "package:datasets"  "package:methods"  
[10] "Autoloads"         "package:base"     
\end{verbatim}
}

\item 注：执行 {\color{blue}\verb+attach(thuesen)+} 的结果是把这个数据框放在搜索路径里。 
{\footnotesize\color{blue}
\begin{verbatim}
> attach(thuesen)
> search()  #可以发现多了 thuesen 这个数据框
 [1] ".GlobalEnv"        "thuesen"           "package:ISwR"     
 [4] "tools:RGUI"        "package:stats"     "package:graphics" 
 [7] "package:grDevices" "package:utils"     "package:datasets" 
[10] "package:methods"   "Autoloads"         "package:base" 
\end{verbatim}
}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{2.1.8.a. 数据框操作函数 subset}

\begin{itemize}
\item {\color{red}问题：选出数据框\, {\color{blue}\verb+thuesen+} 中的 blood.glucose 小于7 的数据。}

\item 解答：使用 \, {\color{blue}\verb+subset()+} 函数，第一个参数是待操作的数据框，第二个参数是所要求的逻辑表达式。

{\footnotesize\color{blue}
\begin{verbatim}
> library(ISwR)
> thue2 <- subset(thuesen, blood.glucose < 7)
> thue2   #结果显示，血糖浓度小于7的有6人。
   blood.glucose short.velocity
6            5.3           1.49
11           6.7           1.25
12           5.2           1.19
15           6.7           1.52
17           4.2           1.12
22           4.9           1.03
\end{verbatim}
}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{2.1.8.b. 数据框操作函数 transform }

\begin{itemize}

\item {\color{red}问题：将数据框\, {\color{blue}\verb+thuesen+} 中的\, {\color{blue}\verb+blood.glucose+} 数据取对数运算，然后添加在这个数据框的新的一列，列名称为\, {\color{blue}\verb+logarithm.of.gluc+}. }

\item 解答：使用\, {\color{blue}\verb+transform()+} 函数，第二个参数定义新的一列数据。 
{\footnotesize\color{blue}
\begin{verbatim}
> thue3 <- transform(thuesen, logarithm.of.gluc = log(blood.glucose))
> thue3  #结果显示，多了右边的第三列数据，为第一列取对数得到
   blood.glucose short.velocity logarithm.of.gluc
1           15.3           1.76          2.727853
2           10.8           1.34          2.379546
3            8.1           1.27          2.091864
...
23           8.8           1.12          2.174752
24           9.5           1.70          2.251292
\end{verbatim}
}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{2.2. 作图系统 The Graphics subsystem}

\begin{itemize}
\item {\color{red}问题：统计图形有哪些特征？}
\item 解答：It is quite common in statistical graphics for you to want to create a plot that is slightly different from the {\color{blue}default (默认)}: Sometimes you will want to add {\color{blue}annotation (注释)}, sometimes you want the {\color{blue}axes (坐标轴)} to be different — {\color{blue}labels (标签)} instead of numbers, irregular placement of {\color{blue}tick marks (坐标刻度)}, etc. 

\vspace{0.5cm}

%\item[2.] 问题：
%\item 解答：

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{2.2.1.a. 图形布局 Plot layout}

\begin{itemize}
\item {\color{red}问题：}

\begin{enumerate} 
\item {\color{red}在 2乘2的正方形中等可能地画50个点。}
\item {\color{red}在给定位置加注记。}
\item {\color{red}画横线和竖线。}
\end{enumerate}

\item 解答：

\begin{lstlisting}[language=R]
x <- runif(50,0,2)  # 生成50个[0,2]之间的均匀分布的小数
y <- runif(50,0,2)  # 生成50个[0,2]之间的均匀分布的小数
plot(x, y, main="Main title", sub="subtitle", 
+	xlab="x-label", ylab="y-label")
# 画出散点图，图的主标题，副标题，对两个坐标轴的描述
text(0.6,0.6,'I am Here!')  # 在指定的位置写文本
abline(h=.6,v=.6)  # 画出竖直线 x=0.6 和 水平线 y=0.6
\end{lstlisting}


\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{2.2.1.b. 图形布局（上一页代码的效果）}

\begin{center}
\includegraphics[height=0.7\textheight, width=0.9\textwidth]{plot2-2-1.png}
\end{center}


\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{2.2.2. 逐步绘制图形 Building a plot from pieces}

\begin{itemize}
\item {\color{red}问题：画出一个空图。然后逐步添加图形元素。}

\item 解答：

\begin{lstlisting}[language=R]
plot(x,y,type='n',xlab='',ylab='',axes=F)  # 结果是个空图
points(x,y)  # 加点
axis(1)  # 加横坐标轴，刻度默认
axis(2,at=seq(0.2,1.8,0.2))  # 加纵坐标轴，刻度给定
box()  # 加方框
title(main='Main Title', sub='subtitle',
+     xlab='xlabel',ylab='ylabel')  # 加标题和坐标标签
\end{lstlisting}

\item 注：结果跟上一页的图类似，区别在于纵坐标轴的刻度。

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{2.2.3. 使用 par 画子图形}

\begin{itemize}
%\item[1.] 问题：函数 \, {\color{blue}\verb+par()+} 能用来做什么？
%\item 解答：The {\color{blue}\verb+par+} settings allow you to control line width and type, character size and font, color, style of axis calculation, size of the plot and figure regions, clipping, etc. 

%\vspace{0.5cm}

\item {\color{red}问题：如何画出几个子图？}

\item 解答：使用函数 \,{\color{blue}\verb+par()+}, 设置参数 \, {\color{blue}\verb+mfrow+} 的值，然后分别画两个图。
%It is possible to divide a figure into several subfigures by using the {\color{blue}\verb+mfrow+} and {\color{blue}\verb+mfcol+} parameters.

\begin{lstlisting}[language=R]
par(mfrow=c(1,2))
plot(x,y)  # 在左边的子图里画50个点
plot(x,x)  # 在右边的子图里画50个点
\end{lstlisting}

\begin{center}
\includegraphics[height=0.35\textheight, width=0.7\textwidth]{plot2-2-3.png}
\end{center}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{2.2.4.a. 组合绘图 Combining plots}

\begin{itemize}
\item {\color{red}问题：将直方图和概率密度函数画在一起。}

\item 解答：
\begin{lstlisting}[language=R]
> x <- rnorm(100)  # 生成标准正态分布的100个随机数
> hist(x,freq=F)  # 画出频率直方图
> curve(dnorm(x), add=T)  # 叠加标准正态分布的密度函数
\end{lstlisting}

\begin{center}
\includegraphics[height=0.4\textheight, width=0.7\textwidth]{plot2-2-4-a.png}
\end{center}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{2.2.4.b. 组合绘图（另一种实现）}

\begin{itemize}


\item {\color{red}问题：下述代码也实现上一页的图形，如何理解 \,{\color{blue}\verb+h+} 这个对象？}
%\item 解答：

\begin{lstlisting}[language=R]
h <- hist(x,plot=F)  # 定义一个直方图对象 h, 但不画图
ylim <- range(0,h$density,dnorm(0))#计算直方图和曲线的上界
hist(x,freq=F,ylim=ylim)  # 画出直方图，纵坐标为频率
curve(dnorm(x),add=T)  # 叠加标准正态分布的密度函数
\end{lstlisting}

\item 注释：对象 \,{\color{blue}\verb+h+} 的 \,{\color{blue}\verb+density+} 变量保存了这个直方图的每一条的高度。
\,{\color{blue}\verb+dnorm(0)+}  是标准正态分布的密度函数在原点的函数值，即这条曲线的最高点的高度。
具体内容可通过命令 \,{\color{blue}\verb+?hist+} 参考帮助文档。\,{\color{blue}\verb+range()+} 取最小值和最大值，本题中则算出直方图和曲线的最高点。 
{\color{blue}
\begin{verbatim}
> range(3,2,5,8)
[1] 2 8
\end{verbatim}
}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{2.3.a. R语言编程（编写一个函数）}

\begin{itemize}
\item {\color{red}问题：演示如何编写一个R函数并运行。}

\item 解答第一步：首先在编辑器里编写下述程序，在工作目录里保存为 hist.with.normal.R 文件。
\begin{lstlisting}[language=R]
hist.with.normal <- function(x,
+ xlab=deparse(substitute(x)),...)
{
    h<-hist(x,plot=F,...)  # 1
    s<-sd(x)  #2
    m<-mean(x)  # 3
    ylim<-range(0,h$density,dnorm(0,sd=s))  # 4
    hist(x,freq=F,ylim=ylim,xlab=xlab,...)  # 5
    curve(dnorm(x,m,s),add=T)  # 6
}
\end{lstlisting}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{2.3.b. R语言编程（运行这个函数）}

\begin{itemize}

\item 解答第二步：在命令行运行这个程序，设输入参数是200个标准正态分布的随机数。
\begin{lstlisting}[language=R]
source('hist.with.normal.R')
hist.with.normal(rnorm(200))
\end{lstlisting}

\item  关于这个函数的注释： 
    \begin{itemize}
    \item 首先是函数名，接着是函数保留字 \,{\color{blue}\verb+function+}, 和第一个输入参数是 \,{\color{blue}\verb+x+}.
    \item 第二部分表示参数 \,{\color{blue}\verb+xlab+} 的默认值是输入  \,{\color{blue}\verb+x+} 的字面内容。
    \item 函数体的第1行是定义一个 \,{\color{blue}\verb+histogram+} 对象，其中 \,{\color{blue}\verb+x+} 是输入的数据。
    \item 第2行和第3行是计算 \,{\color{blue}\verb+x+} 的标准差和均值。
    \item 第4行是计算这个直方图与正态分布的密度函数的最大高度，作为上界。
    \item 第5行和第6行是画出这个直方图和正态分布的密度曲线。
    \end{itemize}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{2.3.c. R语言编程（运行结果）}

\begin{center}
\includegraphics[height=0.7\textheight, width=0.9\textwidth]{plot2-3-a.png}
\end{center}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{2.3.1.a. 流程控制 Flow control （问题与算法）}

\begin{itemize}
\item {\color{red}问题：编程计算平方根 $\sqrt{12}$. 
（$a=12$, 取初始值 $x_0=a/2=6$）}

\item 解答：
\begin{enumerate}
\item  为求 $f(x)=0$ 的根，首先设初始值 $x_0$, 然后使用牛顿法的迭代公式为 
$$x_{n+1} = x_n - \frac{f(x_n)}{f\, '(x_n)}.$$ 
在下一页的插图中，左右两个圆点的横坐标分别是 $x_{n+1}$ 和 $x_n$.

\item  现设特例 $f(x)=x^2-a$, 则有迭代公式 
$$x_{n+1}=x_n-\frac{x_n^2-a}{2x_n} = \frac{x_n}{2} + \frac{a}{2x_n}.$$ 
\end{enumerate}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{2.3.1.b. 流程控制 （算法图示：牛顿法）}

\begin{center}
\includegraphics[height=0.7\textheight, width=0.9\textwidth]{newton-method-sqrt.png}
\end{center}


\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{2.3.1.c. 流程控制（使用 while 循环语句）}

\begin{itemize}

\item 解答一：使用 \,{\color{blue}\verb+while()+} 语句。
\begin{lstlisting}[language=R]
a <- 12  # 设定待求根的数
x <- a/2  # 设定初始值 
# 当 x^2 与 a 的差别超过 10^{-5} 时，要继续迭代：
while(abs(x*x-a)>1e-5) x <- x/2 + a/2/x
x 
[1] 3.464102 （计算结果）
x*x
[1] 12 （验证计算结果的平方）
\end{lstlisting}

\item  注释：\,{\color{blue}\verb+abs( )+} 是求绝对值。

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{2.3.1.d. 流程控制（使用 repeat 循环语句）}

\begin{itemize}

\item 解答二：使用 \,{\color{blue}\verb+repeat()+} 语句。在循环中必须有某个地方可以跳出循环。

\begin{lstlisting}[language=R]
a <- 12
x <- a/2
repeat{
+ x <- (x+a/x)/2
+ # 当误差小于 10^{-5} 时跳出循环
+ if (abs(x*x-a)<1e-5) break  
+ }

x
[1] 3.464102
\end{lstlisting}

\item  注释：\,{\color{blue}\verb+if ( )+} 是判断语句，条件放在小括号内。

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{2.3.1.e. 流程控制（使用 for 循环语句）}

\begin{itemize}

\item {\color{red}问题：画出一组函数 $y=x^n, n=2,3,\cdots,8$ 的图像，设 $0\le x\le 1$. }

\item 解答：函数 \,{\color{blue}\verb+lines()+} 把一些点连起来，第一个参数是这些点的横坐标，第二个参数是这些点的纵坐标。

\begin{lstlisting}[language=R]
> x <- seq(0,1,0.05)  # 设定自变量在 [0,1] 区间的离散取值
> x
 [1] 0.00 0.05 0.10 0.15 0.20 0.25 0.30 0.35 0.40 0.45
[11] 0.50 0.55 0.60 0.65 0.70 0.75 0.80 0.85 0.90 0.95
[21] 1.00
> plot(x,x,ylab='y',type='l')  # 首先画出 y=x 图像
> # 接着画出 y=x^2, ..., y=x^8 的图像
> for (j in 2:8) lines(x,x^j)  
\end{lstlisting}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{2.3.1.f. 流程控制（上页程序结果）}

\begin{center}
\includegraphics[height=0.8\textheight, width=0.5\textwidth]{plot2-3-1-e.png}
\end{center}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{2.3.2. 类与类函数 Classes and generic functions}

\begin{itemize}
\item {\color{red}问题：什么是面向对象程序设计？}

\item 解答：{\color{blue}Object-oriented programming} is about creating coherent systems of {\color{blue}data} and {\color{blue}methods} that work upon them. One purpose is to simplify programs by accommodating the fact that you will have conceptually similar methods for different types of data, even though the implementations will have to be different. 

\item {\color{red}问题：如何获取对象的类？}

\item 解答：The S3 object system is a simple system in which an object has a {\color{blue}\texttt{class}} attribute, which is simply a character vector. One example of this is that all the return values of the classical tests such as {\color{blue}\texttt{t.test}} have class "{\color{blue}\texttt{htest}}", indicating that they are the result of a hypothesis test. 

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{2.4. 数据输入 Data entry}

\begin{itemize}
\item {\color{red}问题：如何获取程序包\, {\color{blue}\texttt{ISwR}} 中的数据集？}

\item 解答：使用\, {\color{blue}\texttt{library(ISwR)}} 命令。其中的数据集参考书本附录B.

\item {\color{red}问题：如何输入一个向量或矩阵？}

\item 解答：使用 \,{\color{blue}\texttt{c()}} 函数。再使用 \,{\color{blue}\texttt{matrix()}} 函数或其它方法。
\begin{lstlisting}[language=R]
> x<-c(2,3,5,7,8,0)  # 输入一个向量
> A<-matrix(x,nrow=2,byrow=T)  # 保存为一个矩阵
> A
     [,1] [,2] [,3]
[1,]    2    3    5
[2,]    7    8    0
\end{lstlisting}

\item 注释：\,{\color{blue}\verb+nrow = number of rows+} 是指定行数，\,{\color{blue}\verb+byrow = T+} 是按行排列。

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{2.4.1.a. 从文本文件读取数据 Reading from a text file}

\begin{itemize}
\item {\color{red}问题：介绍一下 RABE 这本书？}

\item 解答：本书为 Samprit Chatterjee 和 Ali S. Hadi 写的 
\begin{center}
\,{\color{blue} Regression Analysis by Example}, 
中文版《例解回归分析》，
\end{center}
是一本较好的回归分析参考书。
本书的主页为：
\begin{center}
\url{http://www1.aucegypt.edu/faculty/hadi/RABE5/}
\end{center}

\item {\color{red}问题：如何查看该书的数据集？ }

\item 解答：浏览上述主页，可以发现其数据集是文本格式。

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{2.4.1.b. 从文本文件读取数据 Reading from a text file}

\begin{itemize}

\item {\color{red}问题：数据较多，如何输入？}

\item 解答：可以使用 \,{\color{blue}\texttt{read.table()}} 函数，从 \,{\color{blue}\texttt{txt}} 和 \,{\color{blue}\texttt{csv}} 等格式的文件读取数据。

\item {\color{red}问题：将数据存为文本文件，再用 \,{\color{blue}\texttt{read.table()}} 函数读取这些数据。}

\item 解答：首先要将数据文件 \,{\color{blue}\texttt{thuesen.txt}} 放在当前目录。
\begin{lstlisting}[language=R]
x <- read.table('thuesen.txt')
x <- read.table('thuesen',header=T)
\end{lstlisting}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{2.4.2.a. 关于read.table的更多细节 Further details }%on read.table}

\begin{itemize}

\item {\color{red}问题：解释 \,{\color{blue}\texttt{read.table()}} 函数中 \,{\color{blue}\texttt{header}} 参数的用法。}

\item 解答：\,{\color{blue}\texttt{header=T/F}}: It gives a logical value indicating whether the file contains the names of the variables as its first line. If missing, the value is determined from the file format: header is set to \,{\color{blue}\texttt{TRUE}} if and only if the first row contains one fewer field than the number of columns.

\item {\color{red}问题：解释 \,{\color{blue}\texttt{read.table()}} 函数中 \,{\color{blue}\texttt{sep}} 参数的用法。}

\item 解答：It gives the field separator character. Values on each line of the file are separated by this character. 
If \,{\color{blue}\texttt{sep = ''}} (the default for \,{\color{blue}\texttt{read.table}}) the separator is \,{\color{blue}\texttt{'white space'}}, that is one or more spaces, tabs, newlines or carriage returns.

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{2.4.2.b. 从 Excel文件读入数据 }%on read.table}

\begin{itemize}

\item {\color{red}问题：如何将 Excel 文件里的数据读入，存为一个数据框？}

\item 解答：数据存于Excel文件，可以先另存为 csv 格式，然后用  \,{\color{blue}\texttt{read.table()}} 读取。
csv 是指 \,{\color{blue}comma separated values}, 字面意思是``用逗号分隔的值''，可以用记事本打开。
读取 csv 文件时，使用 \,{\color{blue}\texttt{sep=','}}. 
\begin{lstlisting}[language=R]
x <- read.table('thuesen.csv', sep=',')
\end{lstlisting}

\item {\color{red}问题：查看 \,{\color{blue}\texttt{read.table()}} 函数的帮助文档，并研究使用方法。}

%\item 解答：
\begin{lstlisting}[language=R]
?read.table
\end{lstlisting}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{2.4.3. 数据编辑器 The data editor}

\vspace{-0.4cm}\noindent\makebox[\linewidth]{\rule{\paperwidth}{0.4pt}}
%%每页详细内容

\begin{itemize}
\item {\color{red}问题：如何像编辑一个表格那样编辑一个数据框？例如 \,{\color{blue}\verb+airquality+}. }

\item 解答：使用 \,{\color{blue}\verb+edit( )+} 函数。下述命令将数据框 \,{\color{blue}\verb+airquality+} 另存为 \,{\color{blue}\verb+aq+}, 然后再加以编辑。%（这样可以避免修改 \,{\color{blue}\verb+airquality+}）
\begin{lstlisting}[language=R]
aq <- edit(airquality)
\end{lstlisting}

\item {\color{red}问题：先定义一个空的数据框，然后对其编辑，添加数据。}

\item 解答：使用 \,{\color{blue}\verb+fix( )+} 函数。
\begin{lstlisting}[language=R]
dd<-data.frame()  # 创建一个空的数据框
dd
# data frame with 0 columns and 0 rows
fix(dd)  # 添加和修改数据
\end{lstlisting}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{2.4.4. 与其它程序语言的接口 Interfacing to other programs}

\begin{itemize}
\item {\color{red}问题：如何读取 SPSS, SAS, Stata, Minitab 等软件保存的数据？}

\item 解答：使用 \,{\color{blue}\texttt{foreign}} 程序包。

\item {\color{red}问题：如何读取 Access 数据库里的数据？}

\item 解答：使用 \,{\color{blue}\texttt{RODBC}} 程序包。

\end{itemize}

\end{frame}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{2.5.1. Exercise.   }

\begin{itemize}
\item  Question. Describe how to insert a value between two elements of a vector at a given position by using the \,{\color{blue}\texttt{append}} function (use the help system to find out). Without \,{\color{blue}\texttt{append}}, how would you do it?

\item  在向量的两个元素之间插入一个值，有哪些不同的方法？其中之一是使用 \,{\color{blue}\texttt{append}} 函数。

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{2.5.1. Exercise.   }

\begin{itemize}

\item Answer. To insert 1.23 between \,{\color{blue}\texttt{x[7]}} and \,{\color{blue}\texttt{x[8]}}:
\begin{lstlisting}[language=R]
x <- 1:10
z <- append(x, 1.23, after=7)
\end{lstlisting}

Otherwise, consider
\begin{lstlisting}[language=R]
z <- c(x[1:7],1.23,x[8:10])
\end{lstlisting}
or, more generally, to insert \,{\color{blue}\texttt{v}} just after index \,{\color{blue}\texttt{k}} (the boundary cases require some care),
\begin{lstlisting}[language=R]
v <- 1.23; k <- 7
i <- seq(along=x)
z <- c(x[i <= k], v, x[i > k])
\end{lstlisting}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{2.5.2. Exercise.   }

\begin{itemize}
\item  Question. Write the built-in data set \,{\color{blue}\texttt{thuesen}} to a Tab-separated text file with \,{\color{blue}\texttt{write.table}}. View it with a text editor (depending on your system). Change the NA value to . (period), and read the changed file back into R with a suitable command. Also try importing the data into other applications of your choice and exporting them to a new file after editing. You may have to remove row names to make this work.

\item  使用 \,{\color{blue}\texttt{write.table}} 函数，将 \,{\color{blue}\texttt{ISwR}} 中的数据框 \,{\color{blue}\texttt{thuesen}} 保存为一个文本文件，每行的数据用制表符进行分隔。


\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{2.5.2. Exercise.   }

\begin{itemize}

\item Answer. (First part only) Use
\begin{lstlisting}[language=R]
write.table(thuesen, file="foo.txt")
# edit the file
read.table("foo.txt", na.strings=".")
\end{lstlisting}

or
\begin{lstlisting}[language=R]
write.table(thuesen, file="foo.txt", na=".")
read.table("foo.txt", na.strings=".")
\end{lstlisting}

(Notice that if you do not edit the file in the first case, then the second column gets read as a character vector.)

\item Answer v2. Use
\begin{lstlisting}[language=R]
write.table(thuesen,file='foo2.txt',sep='\t')
\end{lstlisting}

\end{itemize}

\end{frame}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{2.6.1. 单项选择题   }

\begin{itemize}

\item %1
下述R语言的哪个命令可以获取当前的工作目录？

\begin{enumerate}[(a)]
\item  {\color{blue}\verb+ getwd()  +}
\item  {\color{blue}\verb+ setwd()  +}
\item  {\color{blue}\verb+ dir() +}
\item  {\color{blue}\verb+ ls() +}
\end{enumerate}


\end{itemize}

\end{frame}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{2.6.1. 单项选择题   }

\begin{itemize}

\item 解答：(a). 
getwd 意思是 get working directory. 即获取工作目录。


\end{itemize}

\end{frame}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{2.6.2. 单项选择题   }

\begin{itemize}

\item %2
下述R语言的哪个命令可以产生标准正态分布的随机数？

\begin{enumerate}[(a)]
\item  \,{\color{blue}\texttt{dnorm(10) }}
\item  \,{\color{blue}\texttt{pnorm(10) }}
\item  \,{\color{blue}\texttt{qnorm(10) }}
\item  \,{\color{blue}\texttt{rnorm(10) }}
\end{enumerate}

\end{itemize}

\end{frame}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{2.6.2. 单项选择题   }

\begin{itemize}

\item  解答：(d). 
r是random number 的首字母。
\begin{enumerate}[(a)]
\item  \,{\color{blue}\texttt{dnorm(10) }}  返回密度函数 $f(x)$ 在 $x=10$ 的函数值。
\item  \,{\color{blue}\texttt{pnorm(10) }}  返回分布函数 $F(x)$ 在 $x=10$ 的函数值，即 $P(X\le 10)$. 
\item  \,{\color{blue}\texttt{qnorm(10) }}  这是求分位数，输入参数是 [0,1] 之间的一个概率。
\item  \,{\color{blue}\texttt{rnorm(10) }} 产生10个随机数。
\end{enumerate}

\end{itemize}

\end{frame}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{2.6.3. 单项选择题   }

\begin{itemize}

\item %3
下述哪个数据框不属于程序包 {\color{blue}\texttt{ISwR}} 的？
\begin{lstlisting}[language=R]
library(ISwR)
\end{lstlisting}

\begin{enumerate}[(a)]
\item  {\color{blue}\verb+thuesen+}
\item  {\color{blue}\verb+energy+}
\item  {\color{blue}\verb+juul+}
\item  {\color{blue}\verb+airquality+}
\end{enumerate}

\end{itemize}

\end{frame}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{2.6.3. 单项选择题   }

\begin{itemize}

\item  解答：(d). 
airquality 是R程序的内建数据框。


\end{itemize}

\end{frame}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{2.6.4. 单项选择题   }

\begin{itemize}

\item %4
运行下述两行R程序之后，下述说法中不正确的是哪个？
\begin{lstlisting}[language=R]
library(ISwR)
attach(thuesen)
\end{lstlisting}

\begin{enumerate}[(a)]
\item  在搜索路径中会出现数据框 \,{\color{blue}\texttt{thuesen}} 和程序包 \,{\color{blue}\texttt{ISwR}}.
\item  可以直接使用数据框 \,{\color{blue}\texttt{thuesen}} 中的变量 \,{\color{blue}\texttt{blood.glucose}} 和 \,{\color{blue}\texttt{short.velocity}}. 
\item  使用 \,{\color{blue}\texttt{detach(thuesen)}} 可以把这个数据框从工作空间移走。
\item  如果原来就定义有 \,{\color{blue}\texttt{blood.glucose}} 变量，则会覆盖这个变量的原来的值。
\end{enumerate}

\end{itemize}

\end{frame}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{2.6.4. 单项选择题   }

\begin{itemize}


\item  解答：(d).
程序运行结果表明，如果变量名本来在数据框之外就独立存在，则 attach 这个数据框之后，该变量保留原来的值。


\end{itemize}

\end{frame}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{2.6.5. 单项选择题   }

\begin{itemize}

\item %5
下述哪个命令找出了数据框 \,{\color{blue}\texttt{thuesen}} 里心室收缩速度大于 1.40 的病人？

\begin{enumerate}[(a)]
\item  \,{\color{blue}\texttt{subset(thuesen,short.velocity>1.40) }}
\item  \,{\color{blue}\texttt{transform(thuesen,short.velocity>1.40) }}
\item  \,{\color{blue}\texttt{within(thuesen,short.velocity>1.40) }}
\item  \,{\color{blue}\texttt{with(thuesen,short.velocity>1.40) }}
\end{enumerate}

\end{itemize}

\end{frame}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{2.6.5. 单项选择题   }

\begin{itemize}

\item  解答：(a). 
测试每个选项，可知第一个完成了任务。这几个函数的含义：
\begin{enumerate}[(a)]
\item  \,{\color{blue}\texttt{subset() }} = subsetting vectors, matrices and data frames
\item  \,{\color{blue}\texttt{transform() }} = transform an object, for example a data frame
\item  \,{\color{blue}\texttt{within() }} = evaluate an expression in a data environment
\item  \,{\color{blue}\texttt{with() }} = evaluate an expression in a data environment
\end{enumerate}



\end{itemize}

\end{frame}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{2.6.6. 单项选择题   }

\begin{itemize}

\item %6
运行下述程序，
\begin{lstlisting}[language=R]
x <- runif(50,0,10)  #1
y <- rnorm(50)  #2
plot(x,y,ylim=c(-3,3))  #3
abline(h=0)  #4
\end{lstlisting}
哪个说法是不正确的？

\begin{enumerate}[(a)]
\item  第1行定义了变量 \,{\color{blue}\texttt{x}} 为区间 [0,10] 上的50个随机数。
\item  第2行定义了变量 \,{\color{blue}\texttt{y}} 为服从标准正态分布的50个随机数。
\item  第3行的 \,{\color{blue}\texttt{ylim}} 参数指定了图像的上下限，从而把50个点都显示出来。
\item  第4行是在原图像的基础上画出 \,{\color{blue}\texttt{x}} 轴。
\end{enumerate}

\end{itemize}

\end{frame}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{2.6.6. 单项选择题   }

\begin{itemize}

\item  解答：(c). 
正态分布的随机数可能不落在区间 [-3,3] 中，因此这50个点不一定都能显示出来。


\end{itemize}

\end{frame}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{2.6.7. 单项选择题   }

\begin{itemize}

\item %7
想在一个图中呈现左右两个子图，下述哪个命令可以做到？

\begin{enumerate}[(a)]
\item  \,{\color{blue}\texttt{par(mfrow=c(1,2))}}
\item  \,{\color{blue}\texttt{par(mfrow=c(2,1))}}
\item  \,{\color{blue}\texttt{par(mfrow=(1,2))}}
\item  \,{\color{blue}\texttt{par(mfrow=(2,1))}}
\end{enumerate}

\end{itemize}

\end{frame}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{2.6.7. 单项选择题   }

\begin{itemize}

\item  解答：(a). 
第2个命令将呈现上下两个子图。


\end{itemize}

\end{frame}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{2.6.8. 单项选择题   }

\begin{itemize}

\item %8
下述程序的运行结果是什么？
%{\color{blue}\begin{verbatim}
%> s<-0
%> k<-1
%> while(s<=100) {s<-s+k; k<-k+1}
%\end{verbatim}
%}

\begin{lstlisting}[language=R]
s=0;k=1;while(s<=100){s=s+k;k=k+1;print(s)}
\end{lstlisting}

\begin{enumerate}[(a)]
\item  5050.
\item  5151.
\item  4950.
\item  105.
\end{enumerate}

\end{itemize}

\end{frame}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{2.6.8. 单项选择题   }

\begin{itemize}


\item  解答：(d). 
注意判别条件是 s 而不是 k. 



\end{itemize}

\end{frame}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{2.6.9. 单项选择题   }

\begin{itemize}

\item %9
关于下述程序，哪个说法是不正确的？
\begin{lstlisting}[language=R]
x <- seq(0,1,0.05)  #1
plot(x,x,ylab='y variable')  #2
for (k in 2:9) lines(x,x/k)  #3
\end{lstlisting}

\begin{enumerate}[(a)]
\item  图像结果显示10条直线。
\item  图像的纵坐标的标签是 y variable.
\item  向量 x 有 21 个分量，正好将 [0,1] 区间 20 等分。
\item  图像结果落在一个单位正方形内。

\end{enumerate}

\end{itemize}

\end{frame}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{2.6.9. 单项选择题   }

\begin{itemize}


\item  解答：(a). 
运行结果表明，第一条是散点，不是直线。

可以设置函数 \,{\color{blue}\texttt{plot()}} 的参数 \,{\color{blue}\texttt{type}} 的值为 \,{\color{blue}\texttt{'l'}} 把散点连起来。

\begin{lstlisting}[language=R]
plot(x,x,ylab='y variable',type='l')
\end{lstlisting}

\end{itemize}

\end{frame}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{2.6.10. 单项选择题   }

\begin{itemize}

\item %10
关于下述程序，哪个说法是不正确的？
\begin{lstlisting}[language=R]
x <- read.table('data01.txt')  #1
y <- read.table('data01.txt', header=T)  #2
z <- read.table('data02.csv', sep=',')  #3
w <- read.table('data02.csv', sep=' ')  #4
\end{lstlisting}

\begin{enumerate}[(a)]
\item  第1行读入一个文本格式的数据框，并且默认没有表头，即文件中的所有行都是数据。
\item  第2行读入一个文本格式的数据框，并且指定文件中的第一行为变量名。
\item  第3行读入一个 csv 格式的数据框，并且指定分隔符为逗号。
\item  第4行读入一个 csv 格式的数据框，并且指定分隔符为空格。
\end{enumerate}

\end{itemize}

\end{frame}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{2.6.10. 单项选择题   }

\begin{itemize}


\item  解答：(a). 
默认有没有表头，是按照文件第一行的长度来决定的。如果第一行的长度比后面几行的长度少一个数据，则默认第一行是表头。



\end{itemize}

\end{frame}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{2.6.11. 单项选择题   }

\begin{itemize}

\item % 11
运行下述程序，哪个说法是不正确的？
\begin{lstlisting}[language=R]
x<-list(u=2,v='abc')
print(x)
class(x)
str(x)
print(x$v)
\end{lstlisting}

\begin{enumerate}[(a)]
\item  第一行程序生成一个列表对象 \,{\color{blue}\texttt{x}}. 
\item  执行命令 \,{\color{blue}\texttt{x[1]}} 和 \,{\color{blue}\verb+x$u+} 的返回结果是一样的。
\item  最后得到的结果是 \,{\color{blue}\texttt{ [1] "abc" }}. 
\item  执行命令 \,{\color{blue}\verb+x$v[2]+} 将返回字符 \,{\color{blue}\texttt{b}}.
\end{enumerate}

\end{itemize}

\end{frame}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{2.6.11. 单项选择题   }

\begin{itemize}


\item  解答：(d). 
逐个运行测试，最后一个选项的返回结果是  \,{\color{blue}\texttt{[1] NA}}.


\end{itemize}

\end{frame}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{2.6.12. 单项选择题   }

\begin{itemize}

\item %12
列表经常用来把不同数据类型的值打包组合在一起。函数的返回值经常是一个列表。这对统计函数特别有用，因为统计函数往往返回一些复杂的结果。测试下述程序，然后选出选项中不正确的那个说法。
\begin{lstlisting}[language=R]
hn <- hist(Nile)
str(hn)
class(hn)
\end{lstlisting}


\begin{enumerate}[(a)]
\item  这个命令的结果是一个直方图，和一个 \,{\color{blue}\texttt{'histogram'}} 类型的列表变量。% \,{\color{blue}\texttt{hn}}. 
\item  用命令 \,{\color{blue}\texttt{length(hn)}} 可知变量 \,{\color{blue}\texttt{hn}} 有6个分量。
\item  列表变量 \,{\color{blue}\texttt{hn}} 的第2个分量 \,{\color{blue}\texttt{hn[2]}} 存储了这个直方图的各条的高度。
\item  使用命令 \,{\color{blue}\texttt{?Nile}} 查看，可知 \,{\color{blue}\texttt{Nile}} 数据保存了尼罗河在1900-2000这些年在开罗的径流量。
\end{enumerate}

\end{itemize}

\end{frame}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{2.6.12. 单项选择题   }

\begin{itemize}


\item  解答：(d). 
查看数据 \,{\color{blue}\texttt{Nile}} 的帮助文档，可知是尼罗河在1871-1970年的在阿斯旺的径流量。

\end{itemize}

\end{frame}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{2.6.13. 单项选择题   }

\begin{itemize}

\item %13
在一个典型的员工数据集里，保存有员工的姓名和年龄等数据。数据框就是用来完成这个任务的。数据框也是一种特殊的列表，其中姓名这个分量保存了所有员工的姓名，年龄保存了所有员工的年龄，性别保存了所有员工的性别。测试下述程序，创建一个数据框。
\begin{lstlisting}[language=R]
emp.names<-c('wang','chen','zhao')
emp.ages<-c(36,37,40)
emp.genders<-c('M','M','F')
emp.list<-list(names=emp.names, ages=emp.ages, 
	+ gender=emp.genders)
emp.df<-data.frame(emp.list)
emp.df
\end{lstlisting}

\end{itemize}

\end{frame}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{2.6.13. 单项选择题   }

\begin{itemize}
\item  选出不正确的说法。
\begin{enumerate}[(a)]
\item  命令 \,{\color{blue}\texttt{emp.df[2]}} 将返回一个只保留所有员工的年龄的数据框。
\item  命令 \,{\color{blue}\texttt{emp.df[2,]}} 将返回员工 chen 所在的数据记录。
\item  命令 \,{\color{blue}\texttt{emp.df[,2]}} 将返回一个只保留所有员工的年龄的向量。
%\item  命令 \,{\color{blue}\texttt{emp.df[c(1,3)]}} 将返回一个只保留所有员工的性别的数据框。
\item  命令 \,{\color{blue}\texttt{emp.df[c(1,3)]}} 将返回第一个员工的性别。
%\item  数据框 \,{\color{blue}\texttt{emp.df}} 的第三列是一个字符型的向量。
\end{enumerate}

\end{itemize}

\end{frame}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{2.6.13. 单项选择题   }

\begin{itemize}


\item  解答：(d). 
这个命令返回的是一个保留所有员工的姓名和性别的数据框。


\end{itemize}

\end{frame}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{2.6.14. 单项选择题   }

\begin{itemize}

\item %14
因子可以简单地看作是一个附加了更多信息的向量。这额外的信息包括向量中不同的值的记录，称为水平。
测试下述程序。
\begin{lstlisting}[language=R]
x <- c(3,12,15,12,15,12)
xf <- factor(x)
xff <- factor(x,levels=c(3,12,15,21))
xf
xff
class(xff)
xfff<-factor(x,levels=c(12,15,21))
\end{lstlisting}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{2.6.14. 单项选择题   }

\begin{itemize}

\item  选出不正确的那个说法。
\begin{enumerate}[(a)]
\item  变量 \,{\color{blue}\texttt{x}} 是一个数值型向量。
\item  变量 \,{\color{blue}\texttt{xf}} 是一个因子型向量，有3个水平。
\item  变量 \,{\color{blue}\texttt{xff}} 是一个因子型向量，有4个水平。
\item  变量 \,{\color{blue}\texttt{xfff}} 是一个因子型向量，有4个水平。
%\item  测试程序的最后结果是一个有4个水平的因子型变量。
\end{enumerate}

\end{itemize}

\end{frame}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{2.6.14. 单项选择题   }

\begin{itemize}


\item  解答：(d). 
创建变量 \,{\color{blue}\texttt{xfff}} 时，设定的水平为 12,15,21, 故数值3被认为是缺失值。
%测试程序的最后结果是一个有4个水平的因子型变量。


\end{itemize}

\end{frame}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{2.6.15. 单项选择题   }

\begin{itemize}

\item %15
测试下述程序，然后判断关于 \,{\color{blue}\texttt{NA}} 和 \,{\color{blue}\texttt{NULL}} 的哪个说法是不正确的。
\begin{lstlisting}[language=R]
x <- c(1,2,3,NA,5)
mean(x)
mean(x,na.rm=T)
y <- NULL
for (k in 1:10) y <- c(y,k*k)
\end{lstlisting}

\begin{enumerate}[(a)]
\item  \,{\color{blue}\texttt{NA}} 表示一个存在但是未知的值。
\item  在数据有 \,{\color{blue}\texttt{NULL}} 作为分量时，返回的结果总是 \,{\color{blue}\texttt{NULL}}. 
\item  在数据有 \,{\color{blue}\texttt{NA}} 作为分量的时候，可用参数 \,{\color{blue}\texttt{na.rm=T}} 来跳过缺失值。
\item  \,{\color{blue}\texttt{NULL}} 表示空值，可用在循环的开始，从 \,{\color{blue}\texttt{NULL}} 开始可以依次添加分量。
\end{enumerate}

\end{itemize}

\end{frame}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{2.6.15. 单项选择题   }

\begin{itemize}

\item  解答：(b). 
在数据有 NULL 时，程序会自动跳过 NULL. 


\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{2.7.1. 简答题   }

\begin{enumerate}

\item  举例说明函数 ls() 和 rm() 、save.image() 函数的用途。

\item  举例说明 getwd() 函数、setwd() 函数、dir() 函数的使用方法。

\item  将当前工作空间保存到一个文件，以便下次继续这次工作。

\item  解释脚本编辑器的使用方法，使用 source() 函数运行脚本文件。

\item  介绍 help.start() 的输出结果。从这里可以看到哪些类型的文档？

\end{enumerate}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{2.7.6. 简答题   }

\begin{enumerate}\setcounter{enumi}{5}

\item  举例说明 help() 函数的使用方法，和在函数名前加‘?’作为前缀来调取帮助文档的使用方法。

\item  举例说明 help.search() 函数的使用方法。

\item  R的程序包一般包含哪些内容？如何安装程序包？举例说明如何使用 library() 函数载入程序包。

\item  用函数 library() 载入 ISwR 包，并用函数 attach() 调入 thuesen 数据框。 

\item  用函数 search() 查看当前搜索路径。

\end{enumerate}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{2.7.11. 简答题   }

\begin{enumerate}\setcounter{enumi}{10}

\item  举例说明 detach() 函数和 with() 函数的使用方法。什么是 GlobalEnv ? 

\item  举例说明 subset() 函数、transform() 函数、within() 函数的使用方法。

\item  举例说明如何画出一个标准的图形：在一个边长为2的正方形内画出 50 个均匀分布的散点，再加上各种标记。

\item 将身高和体重作为横坐标和纵坐标，将某班级学生的身高和体重画成散点图。并加上标注，例如编号。

\item 生成服从标准正态分布的 100 个随机数，画出直方图。叠加一个标准正态分布的密度函数图像。

\end{enumerate}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{2.7.16. 简答题   }

\begin{enumerate}\setcounter{enumi}{15}

\item  举例说明，如何首先绘制不包含任何元素的图形，然后逐步添加元素：使用 plot() 函数、points() 函数、axis() 函数、box() 函数、title() 函数等。使用 par() 函数来修饰图形的各种细节。

\item  使用 rnorm() 函数、hist() 函数、curve() 函数，dnorm() 函数、range() 函数，生成服从标准正态分布的 100 个随机数，画出直方图。叠加一个标准正态分布的密度函数图像。

\item  如何创建一个R函数？使用 while 语句、repeat 语句 和 for 语句编写R程序。

\item 分别使用 while 语句，repeat 语句 和 for 语句，计算 $1+2+\cdots+100$ 的值。

\item  什么是R语言的类？函数 t.test() 的输出结果是个列表。该列表包含了哪些内容？如何提取它们？

\end{enumerate}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{2.7.21. 简答题   }

\begin{enumerate}\setcounter{enumi}{20}

\item  使用 read.table() 函数从本地磁盘读入存有数据的 txt 文件和 csv 文件。解释参数 header, sep, na.strings 的不同取值的含义。

\item 使用 edit() 函数，编辑一个数据框。

\item 使用 write.table() 函数将一个数据框里的数据保存为 txt 文件和 csv 文件。


\end{enumerate}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\end{document}
